<?php
class ModelCatalogSupplier extends Model {
	public function addSupplier($data) {
		$this->db->insert("supplier", $data);
	}
	
	public function editSupplier($supplier_id, $data) {
		$this->db->update("supplier", $data, "supplier_id=$supplier_id");
	}
	
	public function deleteSupplier($supplier_id) {
		$this->db->delete('supplier', "supplier_id = $supplier_id");
	}	
	
	public function getSupplier($supplier_id) {
		return $this->db->get('supplier', "supplier_id = $supplier_id");
	}
	
	public function getSupplierOptions() {
		return $this->db->queryArray("SELECT `supplier_id`, `name` FROM @@supplier", 'supplier_id', 'name');
	}
	
	public function getTotalSuppliers($filter = null) {
		$sql = "SELECT count(*) FROM @@supplier";
		if ($filter && $filter->get('keyword')) {
			$sql .= " WHERE CONCAT(name,contact,address,email) LIKE '%" . ES($filter->get('keyword')) . "%'";
		}
		return $this->db->queryOne($sql);
	}
	
	public function getSuppliers($filter = null) {
		$sql = "SELECT * FROM @@supplier";
		if ($filter) {
			if ($filter->get('keyword')) {
				$sql .= " WHERE CONCAT(name,contact,address,email) LIKE '%" . ES($filter->get('keyword')) . "%'";
			}
			$sql .= $filter->sort('name');
			$sql .= $filter->limit();
		}
		$result = $this->db->query($sql);
		return $result->rows;
	}
	
	function getTotalProductBySupplierId($id) {
		return $this->db->queryOne("SELECT count(*) FROM @@product WHERE supplier_id=$id");
	}
}
?>